package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.NonFinanceTxnMapper;
import cc.rengu.oltp.service.common.entity.NonFinanceTxn;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 非金融交易流水表操作接口实现类
 * Created by 王成 on 2018/4/18.
 */
public class NonFinanceTxnMapperImpl implements NonFinanceTxnMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public NonFinanceTxnMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertNonFinanceTxn(NonFinanceTxn nonFinanceTxn) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertNonFinanceTxn");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, nonFinanceTxn, null);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("登记非金融交易流水表失败,instId:<{}>,sysSeqNum:<{}>,txnDate:<{}>,txnTime:<{}>,txnNum:<{}>,msgSrcId:<{}>,transChannelId:<{}>,frontSeqNum:<{}>,iReturnCode:<{}>",
                    nonFinanceTxn.getInstId(), nonFinanceTxn.getSysSeqNum(), nonFinanceTxn.getTxnDate(), nonFinanceTxn.getTxnTime(), nonFinanceTxn.getTxnNum(), nonFinanceTxn.getMsgSrcId(), nonFinanceTxn.getTransChannelId(), nonFinanceTxn.getFrontSeqNum(), iReturnCode);
            return iReturnCode;
        }
        return 0;
    }

    @Override
    public int updateNonFinanceTxnByPrimaryKey(NonFinanceTxn nonFinanceTxn) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateNonFinanceTxnByPrimaryKey");
        nonFinanceTxn.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, nonFinanceTxn, null);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateNonFinanceTxnHisByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, nonFinanceTxn, null);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("更新非金融交易流水表失败,instId:<{}>,sysSeqNum:<{}>,txnDate:<{}>,txnTime:<{}>,txnNum:<{}>,msgSrcId:<{}>,transChannelId:<{}>,frontSeqNum:<{}>，iReturnCode:<{}>",
                        nonFinanceTxn.getInstId(), nonFinanceTxn.getSysSeqNum(), nonFinanceTxn.getTxnDate(), nonFinanceTxn.getTxnTime(), nonFinanceTxn.getTxnNum(), nonFinanceTxn.getMsgSrcId(), nonFinanceTxn.getTransChannelId(), nonFinanceTxn.getFrontSeqNum(), iReturnCode);
                return iReturnCode;
            }
        }
        return 0;
    }

    @Override
    public NonFinanceTxn selectNonFinanceTxnByFrontSeqNum(String instId, String transDateTime, String frontSeqNum, String msgSrcId, String transChannelId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectNonFinanceTxnByFrontSeqNum");
        NonFinanceTxn nonFinanceTxn = new NonFinanceTxn();
        nonFinanceTxn.setInstId(instId);
        nonFinanceTxn.setTransDateTime(transDateTime);
        nonFinanceTxn.setFrontSeqNum(frontSeqNum);
        nonFinanceTxn.setMsgSrcId(msgSrcId);
        nonFinanceTxn.setTransChannelId(transChannelId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, nonFinanceTxn, nonFinanceTxn);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.warn("根据前端流水号查询非金融交易流水表失败,instId:<{}>,transDateTime:<{}>,frontSeqNum:<{}>,msgSrcId:<{}>,transChannelId:<{}>,iReturnCode:<{}>", instId, transDateTime, frontSeqNum, msgSrcId, transChannelId, iReturnCode);
            return null;
        }
        return nonFinanceTxn;
    }

    @Override
    public NonFinanceTxn selectNonFinanceTxnInfoByPrimaryKey(String instId, String sysSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectNonFinanceTxnByPrimaryKey");
        NonFinanceTxn nonFinanceTxn = new NonFinanceTxn();
        nonFinanceTxn.setInstId(instId);
        nonFinanceTxn.setSysSeqNum(sysSeqNum);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, nonFinanceTxn, nonFinanceTxn);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.warn("根据系统流水号查询非金融交易流水表失败,instId:<{}>,sysSeqNum:<{}>,iReturnCode:<{}>", instId, sysSeqNum, iReturnCode);
            return null;
        }
        return nonFinanceTxn;
    }

    @Override
    public NonFinanceTxn selectNonFinanceTxnByChannelSeqNum(String instId, String channelSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectNonFinanceTxnByChannelSeqNum");
        NonFinanceTxn nonFinanceTxn = new NonFinanceTxn();
        nonFinanceTxn.setInstId(instId);
        nonFinanceTxn.setChannelSeqNum(channelSeqNum);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, nonFinanceTxn, nonFinanceTxn);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.warn("根据通道流水号查询非金融交易流水表失败,instId:<{}>,channelSeqNum:<{}>,iReturnCode:<{}>", instId, channelSeqNum, iReturnCode);
            return null;
        }
        return nonFinanceTxn;
    }
}
